// @flow
import React, { Component } from 'react'
import styles from './FaultGrid.css'
import FaultActions from './FaultActions1'
import AgGrid from 'components/AgGrid'
import moment from 'moment'
import Pagination from 'components/Pagination'

type Props = {
  rowData: Array<Object>,
  refreshFaults: Function,
  history: Object,
  total: number,
  current: number,
  pageSize: number
}
type States = {
  columnDefs: Array<Object>
}
class FaultGrid1 extends Component {
  props: Props
  state: States
  constructor (props: Props) {
    super(props)
    this.state = {
      columnDefs: [
        {
          headerName: '故障名称',
          field: 'eventName',
          cellStyle: {
            padding: '8px'
          }
        }, {
          headerName: '客户',
          field: 'customerId',
          cellStyle: {
            padding: '8px'
          },
          cellRenderer: (params) => {
            if (params.data.customer != null) {
              return params.data.customer.name
            }
            return ''
          }
        }, {
          headerName: '设备',
          field: 'deviceId',
          cellStyle: {
            padding: '8px'
          },
          cellRenderer: (params) => {
            if (params.data.device != null) {
              return params.data.device.name
            }
            return ''
          }
        }, {
          headerName: '故障类型',
          field: 'eventType',
          cellStyle: {
            padding: '8px'
          }
        }, {
          headerName: '故障开始时间',
          field: 'startTime',
          cellStyle: {
            padding: '8px'
          },
          cellRenderer: (params) => {
            return moment(params.data.startTime, 'YYYY-MM-DDTHH:mm:ss.SSSZ').format('YYYY-MM-DD HH:mm:ss')
          }
        }, {
          headerName: '故障结束时间',
          field: 'endTime',
          cellStyle: {
            padding: '8px'
          },
          cellRenderer: (params) => {
            return moment(params.data.endTime, 'YYYY-MM-DDTHH:mm:ss.SSSZ').format('YYYY-MM-DD HH:mm:ss')
          }
        },
        {
          headerName: '负责人',
          field: 'owner',
          cellStyle: {
            padding: '8px'
          }
        },
        {
          headerName: '状态',
          field: 'eventStatus',
          cellStyle: {
            padding: '8px'
          }
        },
        {
          headerName: '操作',
          minWidth: 150,
          cellRendererParams: {
            history: this.props.history,
            refreshCustomers: this.props.refreshFaults
          },
          cellRendererFramework: FaultActions,
          cellStyle: {
            padding: '8px',
            'text-align': 'center'
          }
        }
      ]
    }
  }
  render () {
    const { columnDefs } = this.state
    const { rowData, current, pageSize, total, refreshFaults } = this.props
    return (
      <div className={styles['main']}>
        <AgGrid {...{
          columnDefs,
          rowData
        }} />
        <Pagination current={current} pageSize={pageSize} total={total} onChange={refreshFaults} />
      </div>
    )
  }
}

export default FaultGrid1
